home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / ka9q / nhclb120.zoo / netuser.c < prev    next >
C/C++ Source or Header  |  1992-06-19  |  4KB  |  205 lines

  1. /* Miscellaneous format conversion subroutines */
  2. /* #define DEBUG=1 */
  3. #include <ctype.h>
  4. #include <stdio.h>
  5.  
  6. #ifdef UNIX
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "unix.h"
  10. #endif /* UNIX */
  11.  
  12. #ifdef    BSD
  13. char *sprintf();
  14. #endif
  15. #include "global.h"
  16. #include "netuser.h"
  17.  
  18. extern char *strchr(), *strrchr();
  19. int net_error;
  20. int xatoi();
  21. #define LINELEN 256
  22.  
  23. /* Convert Internet address in ascii dotted-decimal format (44.0.0.1) to
  24.  * binary IP address
  25.  */
  26. int32
  27. aton(t)
  28. char *t;
  29. {
  30.     int32 n;
  31. /*    int atoi(); */
  32.     register int i;
  33.     char x[40];
  34.     char *cp,*s ;
  35.     strncpy(x,t,39);
  36.     x[39]='\0';      /* string length paranoia */
  37.     cp = s =x;
  38.     /* atoi in some libs barfs if supplied with non numeric chars */
  39.     while(*cp){
  40.       if( *cp == '.' || ( *cp >='0' && *cp <='9') ) cp++;
  41.       else *cp ='\0';
  42.     }
  43. #ifdef  DEBUG
  44.     printf("dave debug: input %s\n",s);
  45. #endif
  46.     n = 0;
  47.     for(i=24;i>=0;i -= 8){
  48. #ifdef DEBUG
  49.       printf("dje debug atoi %s\n ",s);
  50. #endif
  51.       n |= (int32)atoi(s) << i;
  52.  
  53. #ifdef DEBUG
  54.       printf("dje int res = %lx \n",n);
  55. #endif
  56.         if((s = index(s,'.')) == NULLCHAR)
  57.         break;
  58.         s++;
  59.     }
  60.     return n;
  61. }
  62. /* Resolve a host name into an IP address. IP addresses in dotted-decimal
  63.  * notation are distinguished from domain names by enclosing them in
  64.  * brackets, e.g., [44.64.0.1]
  65.  */
  66. int32
  67.   xresolve(host)         /* not used - see domain.c */
  68. char *host;
  69. {
  70.     register char *cp,*cp1;
  71.     int i;
  72.     char hostent[LINELEN];
  73.     FILE *sfile;
  74.     static struct {
  75.         char *name;
  76.         int32 address;
  77.     } cache;
  78.  
  79.     if(*host == '['){
  80.         /* Brackets indicate IP address in dotted-decimal form */
  81.         return aton(host + 1);
  82.     }
  83.     if(cache.name != NULLCHAR && strcmp(cache.name,host) == 0)
  84.         return cache.address;
  85.  
  86.     /* Not a numerical IP address, search the host table */
  87.     if((sfile = fopen(hosts,"r")) == NULL){
  88.       printf(" no hosts.net !\n");
  89.         return 0;
  90.     }
  91.     while (!feof(sfile)){
  92.         fgets(hostent,LINELEN,sfile);
  93.         rip(hostent);
  94.         cp = hostent;
  95.         if(*cp == '#' || !isdigit(*cp))
  96.             continue;    /* Comment or invalid line */
  97.         /* in next line "while(*cp" was "while(cp"   dje */
  98.         while(*cp != '\0' ){
  99.             /* Skip white space */
  100.             while(*cp == ' ' || *cp == '\t')
  101.                 cp++;
  102.             if(*cp == '\0')
  103.                 break;
  104.             /* Look for next token, find length of this one */
  105.             if((cp1 = index(cp,'\t')) != NULLCHAR){
  106.                 i = cp1 - cp;
  107.             } else if((cp1 = index(cp,' ')) != NULLCHAR) {
  108.                 i = cp1 - cp;
  109.             } else {
  110.                 i = strlen(cp);
  111.             }
  112.             printf("hostent: [%s], name [%s]\n",hostent,cp);
  113.             if(strlen(host) == i && strncasecmp(host,cp,i) == 0){
  114.                 /* Found it, put in cache */
  115.                 fclose(sfile);
  116.                 if(cache.name != NULLCHAR)
  117.                     free(cache.name);
  118.                 cache.name = malloc((unsigned)strlen(host)+1);
  119.                 strcpy(cache.name,host);
  120.                 cache.address = aton(hostent);
  121.                 return cache.address;
  122.             }
  123.             /* That one didn't match, try the next one */
  124.             cp = cp1;
  125.         }
  126.     }
  127.     /* No address found */
  128.     fclose(sfile);
  129.     return 0;
  130. }
  131.  
  132. /* Convert an internet address (in host byte order) to a dotted decimal ascii
  133.  * string, e.g., 255.255.255.255\0
  134.  */
  135. char *
  136. inet_ntoa(a)
  137. int32 a;
  138. {
  139.     static char buf[16];
  140.  
  141.     sprintf(buf,"%u.%u.%u.%u",
  142.         hibyte(hiword(a)),
  143.         lobyte(hiword(a)),
  144.         hibyte(loword(a)),
  145.         lobyte(loword(a)) );
  146.     return buf;
  147. }
  148. /* Convert a socket (address + port) to an ascii string of the form
  149.  * aaa.aaa.aaa.aaa:ppppp
  150.  */
  151. char *
  152. psocket(s)
  153. struct socket *s;
  154. {
  155.     static char buf[30];
  156.  
  157.     sprintf(buf,"%s:%u",inet_ntoa(s->address),s->port);
  158.     return buf;
  159. }
  160. /* Convert hex-ascii string to long integer */
  161. long
  162. htol(s)
  163. char *s;
  164. {
  165.     long ret;
  166.     char c;
  167.  
  168.     ret = 0;
  169.     while((c = *s++) != '\0'){
  170. #if    (!ATARI_ST && !LATTICE)    /* DG2KK "ik versta er heelemal niets van!" */
  171.         c &= 0x7f;
  172. #endif
  173.         if(c >= '0' && c <= '9')
  174.             ret = ret*16 + (c - '0');
  175.         else if(c >= 'a' && c <= 'f')
  176.             ret = ret*16 + (10 + c - 'a');
  177.         else if(c >= 'A' && c <= 'F')
  178.             ret = ret*16 + (10 + c - 'A');
  179.         else
  180.             break;
  181.     }
  182.     return ret;
  183. }
  184. int xatoi(s) 
  185. #if __STDC__ > 0
  186. const  char *s;
  187. #else
  188. char *s;
  189. #endif
  190.  
  191. {
  192.   /* replacement atoi */
  193.   int n=0;
  194.   while(1)   /* Note: compiling with GCC-ST,1.4 seems to require while(1) ,
  195.           *  putting  while(*s) does not work.
  196.           * last character befor null does not get counted !
  197.           * You tell me what's going on ?
  198.           */
  199.     {
  200.     if( !( *s >='0' && *s <='9')) return(n);
  201.     n =  (n *10) + *s - '0';
  202.     s++;
  203.   }
  204. }
  205.